package com.xindaoapp.happypet.utils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.accounts.NetworkErrorException;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.xindaoapp.happypet.R;

public class BitmapUtil {
    /**
     * 获取bitmap
     * 
     * @param pic
     * @return
     * @throws NetworkErrorException
     * @throws IOException
     */
    public static Bitmap getBitMapFormURL(String pic) throws NetworkErrorException, IOException {
        URL picUrl = new URL(pic);
        HttpURLConnection conn = (HttpURLConnection) picUrl.openConnection();

        conn.setConnectTimeout(500);
        conn.setReadTimeout(500);
        // 获取返回码
        int code = conn.getResponseCode();
        if (code == 200) {
            InputStream in = conn.getInputStream();
            Bitmap bm = BitmapFactory.decodeStream(in);
            return bm;
        } else {
            throw new NetworkErrorException("网络连接错误");
        }
    }

    public static DisplayImageOptions options;

    public static DisplayImageOptions getPersonLoading() {
        if (options == null) {
            options =
                    new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
                            .bitmapConfig(Config.RGB_565).imageScaleType(ImageScaleType.NONE)
                            .showImageForEmptyUri(R.drawable.person_loading)
                            .showStubImage(R.drawable.person_loading)
                            .showImageOnFail(R.drawable.person_loading).cacheInMemory(true)
                            .cacheOnDisc(true).build();
        }
        return options;
    }

    public static DisplayImageOptions options3;

    public static DisplayImageOptions getMyCustomLoading() {
        if (options3 == null) {
            options3 =

                    new DisplayImageOptions.Builder()
                            .resetViewBeforeLoading(true)
                            .imageScaleType(ImageScaleType.NONE)
                            // .displayer(new FadeInBitmapDisplayer(1000))
                            .bitmapConfig(Bitmap.Config.RGB_565)
                            .showImageForEmptyUri(R.drawable.person_loading)
                            .showImageOnFail(R.drawable.person_loading)
                            .showStubImage(R.drawable.person_loading).cacheInMemory(true)
                            .cacheOnDisc(true).build();
        }
        return options3;
    }

    public static DisplayImageOptions options1;

    public static DisplayImageOptions getPetLoading() {
        if (options1 == null) {
            options1 =
                    new DisplayImageOptions.Builder().resetViewBeforeLoading(true)
                            .bitmapConfig(Config.RGB_565).imageScaleType(ImageScaleType.NONE)
                            .showImageForEmptyUri(R.drawable.pet_touxiang)
                            .showStubImage(R.drawable.pet_touxiang)
                            .showImageOnFail(R.drawable.pet_touxiang).cacheInMemory(true)
                            .cacheOnDisc(true).build();
        }
        return options1;
    }

}
